<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("player_profile_backup", $con);

$award_title = $_GET['title'];
$fn = $_GET['fileName'];
//$teamOrCollege = $_GET["team_or_college"];
$teamOrCollege = 'college';
$type = 'college';
$handle = fopen($fn, "r");
echo "filename: $fn<br>";

$duplicates = array();

if ($handle) {
	//echo "$award_title Winners:<br>";
    while (($buffer = fgets($handle, 4096)) !== false) {
        $line = explode(',', $buffer);
		$year = trim($line[0]);
		//echo "$year: ";
		$name = trim($line[1]);
		$names = explode(' ', $name);
		if(sizeof($names) > 2){
			$first = join(" ", array_slice($names, 0, sizeof($names) - 1));
			$last = $names[sizeof($names) - 1];
		}
		else{
			$first = $names[0];
			$last = $names[1];
		}
		$first = str_replace("\"", "", $first);
		$first = str_replace("'", "\'", $first);
		$last = str_replace("\"", "", $last);
		$last = str_replace("'", "\'", $last);
		$college = str_replace("\"", "", trim($line[2]));
		//echo "First: /$first/, Last: /$last/, $teamOrCollege: /$college/<br>";
		
		$player_select = " 	SELECT id FROM players
							WHERE first_name='$first' and last_name='$last' and $teamOrCollege like '%$college%'";
		$result = mysql_query($player_select);
		//echo "R: ".var_dump($r)."<br>";
		$r = mysql_fetch_assoc($result);
		if($r != null){
			$id = $r['id'];
			echo "<span style='color:green;'>$first $last, $college Exact match</span><br>";
		}				
		
		if(!$r){
			echo "<span style='color:red;'>NEW $first $last $college</span> <br>";
			$player_insert = "	INSERT into players (first_name, last_name, $teamOrCollege)
								VALUES ('$first', '$last', '$college')";
			//echo "<span style='color:red;'>Player insert</span>:<br>$player_insert <br>";
			mysql_query($player_insert);
			$id = mysql_insert_id();			
		}			
		//echo "<br>Player: $id, $first, $last, $college<br> Award: $award_title<br>";
		if(!$id){
			continue;
		}
		//check if award is already there
		$award_select = "Select * from awards where award_title = '$award_title' and year = $year and player_id = $id and type='$type'";
		$result = mysql_query($award_select);
		$r = mysql_fetch_assoc($result);
		$a_id = $r['id'];
		if($a_id != 0 && $a_id != $id){
			//the award returned an actual id, so it exists
			echo "<span style='color:red;'>Award Exists</span> <br>";
			continue;
		}
		echo "<span style='color:blue;'>Insert new Award $award_title</span><br>";
		$query = "	INSERT into awards (award_title, player_id, year, type) 
					VALUES ('$award_title', $id, $year, '$type')";
		//echo "<span style='color:blue;'>Award insert</span>:<br> $query<br><br>";
		$result = mysql_query($query);
		echo "<br>";
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
else{
	echo var_dump($php_errormsg);
	echo "failed to read file";
}
// $length = sizeof($duplicates);
// if($length > 0){
	// echo "<form action='import_duplicates.php'>";
	// echo "<table>\n";
	// echo "<tr><th>New Player</th><th>Existing Player</th><th>id</th><th>Player Name</th><th>College</th><th>Team</th><th>Query Info</th></tr>\n";
// }	
// for($i = 0; $i < $length; $i++){
	// $name = $duplicates[$i]['first_name']." ".$duplicates[$i]['last_name'];
	// $college = $duplicates[$i]['college'];
	// $team = $duplicates[$i]['team'];
	// $id = $duplicates[$i]['id'];
	// $query_info = $duplicates[$i]['query_teamOrCollege'];
	// echo "<tr>";
	// echo "<td>"."<input type='radio' name='$id' value='add'/>New?"."</td>";
	// echo "<td>"."<input type='radio' name='$id' value='overwrite'/>Existing?"."</td>";
	// echo "<td>$id</td>";
	// echo "<td>$name</td>";
	// echo "<td>$college</td>";
	// echo "<td>$team</td>";
	// echo "<td>$query_info</td>";
	// echo "</tr>\n";
// }
// if($length > 0){
	// echo "</table>\n";
	// echo "</form>\n";
// }

?>